<?php

declare(strict_types=1);

namespace app\index\controller;

use app\common\model\TrandingbankAllowgoods as M;
use app\common\model\TrandingbankListsr as listM;
use app\common\service\TrandingbankListsr as listS;

use think\facade\Db;

//访问地址：http://localhost/index.php/handingbank/函数名
//访问页面：//http://101.33.245.40:7999/index.php/handingbank
class Handingbank extends \app\BaseController
{

    public function index($actorid, $serverid)
    {
        return $this->fetch('', ['GoodsLs' => listM::getGoodsls()]);
    }

    public function test()
    {
        return json(['msg' => 'test2！', 'sellactorid' => '2294761', 'code' => 200]);
    }

    //验证物品和最大数量
    public function checkgoods($goodsid, $goodsnum)
    {
        $goods = M::checkdate($goodsid);

        if (empty($goods)) {
            return json(['msg' => '物品不支持出售！', 'code' => 2]);
        }

        if ($goodsnum > $goods["maxnum"]) {
            return json(['msg' => '物品数量超出最大数量！', 'code' => 2]);
        } else {
            return json(['msg' => '验证无误！', 'handingfees' => $goods['handingfees'], 'code' => 1]);
        }
    }

    //获取单个交易物品
    public function getgoods($id)
    {
        $data = listM::checkStatus($id);
        if ($data != 0) {
            return json($data);
        } else {
            return 0;
        }
    }

    //出售物品
    public function shellGoods($serverid, $actorid, $goodsid, $goodsnum, $goodsname, $goodsprice, $handingfees)
    {

        $playerInfo = listM::getPlayerInfo($serverid, $actorid);

        $data = [
            'sellplayaccount' => $playerInfo["accountname"],
            'shellplayactorid' => $actorid,
            'shellplaynikename' => $playerInfo["actorname"],
            'shellplayserverid' => $serverid,
            'shellplaygrotime' => date('Y-m-d H:i:s'),
            'shellgoodsid' => $goodsid,
            'shellgoodsname' => $goodsname,
            'shellgoodsnum' => $goodsnum,
            'shellgoodsprice' => $goodsprice,
            'goodshandingfees' => $handingfees,
            'goodstatus' => 1,
        ];

        listM::create($data);
        return json(['msg' => '111！', 'code' => 1]);
    }

    //购买物品
    public function buyGoods($serverid, $actorid, $handingid)
    {

        $playerInfo = listM::getPlayerInfo($serverid, $actorid);

        // return json(['msg' => '111！', 'code' => $playerInfo["accountname"]]);

        $goodsData = listM::checkStatus($handingid);

        $goodsData["buyplayaccount"] = $playerInfo["accountname"];
        $goodsData["buyplayactorid"] = $actorid;
        $goodsData["buyplaynikename"] = $playerInfo["actorname"];
        $goodsData["buyplayserverid"] = $serverid;
        $goodsData["buyplaygortime"] = date('Y-m-d H:i:s');
        $goodsData["goodstatus"] = 2;
        listM::update($goodsData);


         // 计算价格并向上取整
         $price = (float)$goodsData["shellgoodsprice"]-$goodsData["goodshandingfees"];

        $idstr = "1,1403010," . $price;



        listM::sendMailBuy($goodsData["shellplayserverid"], $goodsData["shellplayactorid"], '金币', $idstr);
    }


    public function getGoodsls()
    {
        $data = listM::getGoodsls();
        // 返回JSON响应
        return json(['msg' => '操作成功', 'data' => $data, 'code' => 1]);
    }
}
